iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 28
1
Mobile Development

安卓新手新手安卓教學系列 第 28

DAY28 第一個完整程式練習,一台計算機!(三)

  • 分享至 

  • xImage
  •  

今天我們要來講剩下的方法

    public void time(View view){
        if (record == 5) {
            record = 2;
        }
        think();
        recordtime = 0;
        record = 2;
        number.setText(temp +"×");
        warming++;
        warm();
    }

先分temp等不等於0和num2不等於0,前者的話temp就等於一,再讓time等於temp,最後歸零num,再判斷num2等不等於0,是的話就temptime=time X num2,不是就temptime = time,這裡我前面少了第一個判斷,卡了很久

    public void devide(View view){
        if (record == 5) {
            record = 3;
        }
        think();
        recordtime = 0;
        record = 3;
        number.setText(temp +"÷");
        warming++;
        warm();
    }

講到除法就很忌諱0,所以前面要先片段是否數字為0,是就變成1,接著就tempdevide = num2 / devide和tempdevide = devide / num2差別,畢竟剛開始按了數字再按除鍵,數字不會變,但還是要紀錄一下,我再打的時候也想了很久

    public  void warm(){
        if (warming > 1){
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("Warming")
                    .setMessage("運算符號輸入兩遍")
                    .setPositiveButton("OK",null)
                    .show();

            number.setText("0");
            num = 0;
            num1 =0 ;
            num2 = 0;
            temp = 0;
            record = 5;
            recordplus = 1;
            recordnegetive = 1;
            recordtime = 1;
            recorddevide = 1;
            plus = 0;
            negetive = 0;
            time = 0;
            devide = 0;
            warming = 0;
        }
    }

這個只是再判斷如果使用者按了兩下運算符號就警告並歸零,沒有很難懂

    public void enter(View view){
        think();
        number.setText(""+temp);
    }

enter按了之後就會顯示答案,蠻簡單的方法
我在下面貼一下完整的程式碼,就不會那麼亂了

        TextView number;
        double num;
        double num1;
        double num2 = 0;

        double tempplus;
        double tempnegetive;
        double temptime;
        double tempdevide;
        double temp = 0;

        double plus =0;
        double negetive =0;
        double time = 1;
        double devide = 1;

        int record = 5;
        int recordplus = 1;
        int recordnegetive =1;
        int recordtime = 1;
        int recorddevide =1;

        int warming = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        number = findViewById(R.id.number);
    }

    public void count(){
        num = (num2 * 10)+ num1;
        num2 = num;
        warming = 0;
    }
    public void one(View view){
        num1 = 1;
        count();
        number.setText(num+"");
    }

    public void two(View view){
        num1 = 2;
        count();
        number.setText(num+"");
    }

    public void three(View view){
        num1 = 3;
        count();
        number.setText(num+"");
    }

    public void four(View view){
        num1 = 4;
        count();
        number.setText(num+"");
    }

    public void five(View view){
        num1 = 5;
        count();
        number.setText(num+"");
    }

    public void six(View view){
        num1 = 6;
        count();
        number.setText(num+"");
    }

    public void seven(View view){
        num1 = 7;
        count();
        number.setText(num+"");
    }

    public void eight(View view){
        num1 = 8;
        count();
        number.setText(num+"");
    }

    public void nine(View view){
        num1 = 9;
        count();
        number.setText(num+"");
    }

    public void zero(View view){
        num1 = 0;
        count();
        number.setText(num+"");
    }

    public void think(){
        if(record == 0){
            plus = temp;
            num = 0;
            tempplus = plus + num2;
            plus = tempplus;
            num2 = 0;
            temp = plus;
        }else if(record == 1){
            if (temp == 0) {
                negetive = temp;
                num=0;
                tempnegetive = num2;
            }else {
                negetive = temp;
                num = 0;
                tempnegetive = negetive - num2;
            }
            negetive = tempnegetive;
            num2 =0;
            temp = negetive;
        }else if(record == 2){
            if(temp == 0 && num2!=0){
                temp = 1;
            }
            time = temp;
            num = 0;
            if(num2!=0) {
                temptime = time * num2;
            }else {
                temptime = time;
            }
            time = temptime;
            num2 = 0;
            temp = time;
        }else if(record == 3){
            if(temp == 0){
                temp = 1;
                devide = temp;
                num = 0;
                tempdevide = num2 / devide;
            }else {
                devide = temp;
                num = 0;
                tempdevide = devide / num2;
            }
            devide = tempdevide;
            num2 = 0;
            temp = devide;
        }
    }
    public  void warm(){
        if (warming > 1){
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("Warming")
                    .setMessage("運算符號輸入兩遍")
                    .setPositiveButton("OK",null)
                    .show();

            number.setText("0");
            num = 0;
            num1 =0 ;
            num2 = 0;
            temp = 0;
            record = 5;
            recordplus = 1;
            recordnegetive = 1;
            recordtime = 1;
            recorddevide = 1;
            plus = 0;
            negetive = 0;
            time = 0;
            devide = 0;
            warming = 0;
        }
    }
    public void plus(View view){
        if (record == 5){
            record = 0;
        }
        think();
        recordplus = 0;
        record = 0;
        number.setText( temp+"+");
        warming++;
        warm();
    }
    public void negetive(View view){
        if (record == 5){
            record = 1;
        }
        think();
        recordnegetive = 0;
        record = 1;
        number.setText( temp + "-");
        warming++;
        warm();
    }
    public void time(View view){
        if (record == 5) {
            record = 2;
        }
        think();
        recordtime = 0;
        record = 2;
        number.setText(temp +"×");
        warming++;
        warm();
    }
    public void devide(View view){
        if (record == 5) {
            record = 3;
        }
        think();
        recordtime = 0;
        record = 3;
        number.setText(temp +"÷");
        warming++;
        warm();
    }
    public void ac(View view){
        number.setText("0");
         num = 0;
         num1 =0 ;
         num2 = 0;
         temp = 0;
         record = 5;
         recordplus = 1;
         recordnegetive = 1;
         recordtime = 1;
         recorddevide = 1;
         plus = 0;
         negetive = 0;
         time = 0;
         devide = 0;
    }
    public void delete(View view){
        if(num >= 10) {
            num = (num - num1)/ 10;
            num2 = num;
            number.setText(num+"");
        }else if(num > 0){
            num = 0;
            num2 = num;
            number.setText(num+"");
         }
    }
    public void enter(View view){
        think();
        number.setText(""+temp);
    }

但畢竟我也是新手,很有可能有邏輯錯誤,因此造成一些奇怪的BUG,所以就請多多包涵囉!


上一篇
DAY27 第一個完整程式練習,一台計算機!(二)
下一篇
DAY29 把程式裝到手機裡吧
系列文
安卓新手新手安卓教學30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言